import { BasicFetchResult } from '../model/baseModel';
import { defHttp } from '@/utils/http/axios';

enum Api {
  AccountList = '/sysUser/page',
  IsAccountExist = '/sysUser/exists',
  DeptList = '/sysOrg/tree',
  EditDept = '/sysOrg/edit',
  DeleteDept = '/sysOrg/delete',
  AddDept = '/sysOrg/add',
  setRoleStatus = '/sysRole/changeStatus',
  MenuList = '/sysMenu/tree',
  EditMenu = '/sysMenu/edit',
  DetailMenu = '/sysMenu/detail',
  DeleteMenu = '/sysMenu/delete',
  AddMenu = '/sysMenu/add',
  RolePageList = '/sysRole/page',
  GetAllRoleList = '/sysRole/list',
  EditRole = '/sysRole/edit',
  AddRole = '/sysRole/add',
  GrantMenu = '/sysRole/grantMenu',
  GrantOrg = '/sysRole/grantOrg',
  DeleteRole = '/sysRole/delete',
  // 字典类型
  DictTypeApi = '/sysDictType',

  SysDictDataApi = '/sysDictData',

  SysOpLogApi = '/sysOpLog',
  SysVisLogApi = '/sysVisLog',

  CodeGenApi = '/codeGenerate',
  CodeGenConfigApi = '/sysCodeGenerateConfig',
}
export const getAccountList = (params: any) =>
  defHttp.get<BasicFetchResult<any>>({
    url: Api.AccountList,
    params,
    data: { ...params },
  });
export const getOrgUsers = (params: any) => {
  return defHttp.get<BasicFetchResult<any>>({
    url: '/sysUser/orgUser',
    params,
  });
};
export const getUserMenus = () => {
  return defHttp.get<BasicFetchResult<any>>({
    url: '/sysUser/userMenus',
  });
};
export const getOrgList = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: '/sysOrg/tree', params });
export const getDeptList = (params?: any) =>
defHttp.get<BasicFetchResult<any>>({ url: '/sysOrg/departTree', params });

export const getMenuList = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: Api.MenuList, params });
export const detailMenu = (id: string) =>
  defHttp.get<BasicFetchResult<any>>({ url: `${Api.DetailMenu}/${id}` });
export const getGrantMenu = (roleId: string) =>
  defHttp.get<BasicFetchResult<any>>({
    url: `/sysRole/getGrantMenu/${roleId}`,
  });
export const editMenu = (params?: any) =>
  defHttp.post({ url: Api.EditMenu, params });

export const addMenu = (params?: any) =>
  defHttp.post({ url: Api.AddMenu, params });

export const deleteMenu = (id: string) =>
  defHttp.post({ url: Api.DeleteMenu, params: { id } });

export const getRoleListByPage = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: Api.RolePageList, params });

export const getAllRoleList = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: Api.GetAllRoleList, params });

export const setRoleStatus = (id: number, status: number) =>
  defHttp.post({ url: Api.setRoleStatus, params: { id, status } });

export const editRole = (params?: any) =>
  defHttp.post({ url: Api.EditRole, params });
export const grantMenu = (params?: any) =>
  defHttp.post({ url: Api.GrantMenu, params });
export const grantOrg = (params?: any) =>
  defHttp.post({ url: Api.GrantOrg, params });
export const listRules = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: '/sysRole/grantRules', params });
export const listOrgs = (roleId: string) =>
  defHttp.get<BasicFetchResult<any>>({ url: `/sysRole/getGrantOrg/${roleId}` });
export const grantRule = (params?: any) =>
  defHttp.post({ url: '/sysRole/grantRule', params });
export const addRole = (params?: any) =>
  defHttp.post({ url: Api.AddRole, params });

export const deleteRole = (id: string) =>
  defHttp.post({ url: Api.DeleteRole, params: { id } });

export const isAccountExist = (username: string) =>
  defHttp.get(
    { url: Api.IsAccountExist, params: { username } },
    { errorMessageMode: 'none' }
  );

export const getSysVisLogPage = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: Api.SysVisLogApi, params });
export const getSysOpLogPage = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({ url: Api.SysOpLogApi, params });

export const getTableList = (params?: any) =>
  defHttp.get<BasicFetchResult<any>>({
    url: Api.CodeGenApi + '/InformationList',
    params,
  });

export const changeServerLocale = (locale: string) => {
  defHttp.post({
    url: '/sysUser/change',
    params: { locale: locale.split('_')[0] },
  });
};
